//
//  YYImagePickerViewController.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/2/7.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

protocol YYImagePickerViewControllerDelegate: NSObjectProtocol {
    
    /**
     *  选择图片结束后，点击完成选项执行的代理方法
     *
     *  - Parameters:
     *      - yyImagePickerViewController: 当前的图片选择控制器
     *      - photos:                      选择的图片对象模型数组
     */
    func yy_imagePickerViewController(_ controller: YYImagePickerViewController, choosedPhotos photos: Array<YYPhotoModel>) -> Void
    
}


class YYImagePickerViewController: UINavigationController, YYPhotoCollectionViewControllerDelegate {
    
    static var delegate: YYPhotoCollectionViewControllerDelegate?
    
    /// 代理属性
    weak var yy_delegate: YYImagePickerViewControllerDelegate? {
        didSet {
            YYImagePickerViewController.delegate = self
        }
    }
    /// 选择图片的最大数量
    var yy_maxPickCount: Int = Int.max {
        willSet {
            YYPhotoHandler.maxPickCount = newValue
        }
    }
    /// 是否要记录图片是否被选择，default is false
    /// 当再次使用该多图片选择器时需要重新赋值(目前只能记录一次), 当删除照片的时候需要删除记录
    var yy_needRecord: Bool = false {
        willSet {
            YYPhotoHandler.isRecord = newValue
            
            if newValue {
                let array: Array<String>? = NSKeyedUnarchiver.unarchiveObject(withFile: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!.appending("/choosedPhotoRecord")) as? Array<String>
                if let array = array {
                    YYPhotoHandler.choosedPhotos = array
                } else {
                    YYPhotoHandler.choosedPhotos = []
                }
            }
        }
    }
    /**
     *  多图片选择器的初始化方法
     *
     *  - Parameters:
     *      - assetCollectionType: 相册列表
     *      - defaultType:         默认跳转的相册类型
     *      - defaultTitle:        默认跳转的相册名
     *
     *  - Returns: 实例对象
     */
    init(assetCollectionTypes: YYPhotoHandler.AssetCollectionType = .default, defaultType: YYPhotoHandler.AssetCollectionType = .default, defaultTitle: String? = nil) {
        let albumTVC: YYAlbumTableViewController = YYAlbumTableViewController(assetCollectionTypes: assetCollectionTypes, defaultType: defaultType, defaultTitle: defaultTitle)
        super.init(rootViewController: albumTVC)
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /**
     *  判断相册读取权限
     *
     *  - Parameters:
     *      - authorized: 授权状态下执行
     *      - denied:     未授权状态下执行
     */
    static func yy_available(authorized: @escaping () -> Void, denied: @escaping () -> Void) -> Void {
        YYPhotoHandler.available(authorized: authorized, denied: denied)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    func photoCollectionViewController(_ controller: YYPhotoCollectionViewController, choosedPhotos photos: Array<YYPhotoModel>) {
        if let delegate = yy_delegate {
            delegate.yy_imagePickerViewController(self, choosedPhotos: photos)
        } else {
            print("没有指定代理对象")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
